package com.xzx.mr.writable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/15 21:30
 */
public class FlowCountReducer extends Reducer<Text, FlowBean, Text, Text> {
    private Text outputValue = new Text();
    private FlowBean flowBean = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException,
            InterruptedException {
        long upFlow = 0;
        long downFlow = 0;
        long sumFlow = 0;
        for (FlowBean flowBean : values) {
            upFlow += flowBean.getUpFlow();
            downFlow+= flowBean.getDownFlow();
        }
        sumFlow = upFlow + downFlow;

        flowBean.setUpFlow(upFlow);
        flowBean.setDownFlow(downFlow);
        flowBean.setSumFlow(sumFlow);

        outputValue.set(flowBean.toString());
        context.write(key, outputValue);
    }
}
